package com.meterreadingsys.util;




import android.content.Context;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import android.widget.Toast;

public class GpsInfo {
	private Double latitude = 0.0;
	private Double longitude = 0.0;
	private Context context;
	public Double getLatitude() {
		return latitude;
	}
	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}
	public Double getLongitude() {
		return longitude;
	}
	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}
	
	public GpsInfo(Context context){
		this.context=context;		
	}
	/**
	 * 判断GPS是否存在或开启
	 */
	public boolean openGPSSettings(){
		 LocationManager alm=(LocationManager)context.getSystemService(context.LOCATION_SERVICE);
		 
		 if(alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)){
			 Toast.makeText(context, "GPS模块正常", Toast.LENGTH_SHORT)
             .show();
           return true;
		 }
		return false;
			 
	}
	
	/**
	 * 创建LocationManager
	 * @return
	 */
	public LocationManager getLocationManager(){
		return (LocationManager)context.getSystemService(context.LOCATION_SERVICE);
		
	}
	
	/**
	 * 设置GPS参数
	 * @return
	 */
	public  Criteria getCriteria(){
		 // 查找到服务信息
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
		return criteria;
	}
	@SuppressWarnings("unused")
	private void getLocation()
    {
        // 获取位置管理服务
        LocationManager locationManager;
        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) context.getSystemService(serviceName);
       
       Criteria criteria=getCriteria();
        String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
        
        
        final Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
        updateToNewLocation(location);
       
       
        // 设置监听器，自动更新的最小时间为间隔N秒(1秒为1*1000，这样写主要为了方便)或最小位移变化超过N米
        locationManager.requestLocationUpdates(provider, 100 * 1000, 500,
                new LocationListener() {
					
					@Override
					public void onStatusChanged(String provider, int status, Bundle extras) {
						System.out.println("onStatusChanged");
						System.out.println(provider+":"+status+":");
						
					}
					
					@Override
					public void onProviderEnabled(String provider) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onProviderDisabled(String provider) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onLocationChanged(Location location) {
						updateToNewLocation(location);
						
					}
				});
    }
	
	/**
	 * 通过Location 取得径维度
	 * @param location1
	 * @return
	 */
	public Double[] updateToNewLocation(Location location1) {

      
        System.out.println("更新地址");
        if (location1 != null) {
              this.latitude = location1.getLatitude();
            this.longitude= location1.getLongitude();
            System.out.println("维度：" +  latitude+ "\n经度" + longitude);
            return new Double[]{location1.getLatitude(),location1.getLongitude()};
           
        } else {
           System.out.println("无法获取");
        }
		return null;

    }
	
}
